
//
//  BezierView.m
//  室内地图测试
//
//  Created by Maybe_文仔 on 2018/6/6.
//  Copyright © 2018年 Maybe. All rights reserved.
//

#import "SeatBezierView.h"

@interface SeatBezierView()
@property (nonatomic, strong) NSMutableArray *pathInfoMArr;
@end

@implementation SeatBezierView

- (void)drawRect:(CGRect)rect {
    
    //设置填充颜色
    [KAPP_Available_COLOR setFill];
    
    for (NSDictionary *dict in self.areaList) {
        //创建UIBezierPath
        UIBezierPath *apath = ({
            
            UIBezierPath *path = [UIBezierPath bezierPath];
            path.lineWidth     = 0.0f;              //设置线条宽度
            path.lineCapStyle  = kCGLineCapRound;   //设置拐角
            path.lineJoinStyle = kCGLineCapRound;  //终点处理
            
            NSArray *areaArr = dict[@"area"];
            for (int i = 0; i < areaArr.count; i++) {
                NSDictionary *dic = areaArr[i];
                if (i == 0) {
                    //设置起始点
                    [path moveToPoint:CGPointMake([dic[@"x"] floatValue], [dic[@"y"] floatValue])];
                }
                else{
                     //增加线条
                    [path addLineToPoint:CGPointMake([dic[@"x"] floatValue], [dic[@"y"] floatValue])];
                }
            }

            //关闭路径
            [path closePath];
            
            path;
        });
        
        NSMutableDictionary *mDic = [NSMutableDictionary new];
        [mDic setObject:apath forKey:@"path"];
        [mDic setObject:dict[@"name"] forKey:@"name"];
        [mDic setObject:dict[@"roomId"] forKey:@"roomId"];
        [self.pathInfoMArr addObject:mDic];
    
        //根据坐标连线
        [apath stroke];
        //根据闭合路线填充色彩
        [apath fill];
    }
    
}

-(void)setAreaList:(NSArray *)areaList{
    _areaList = areaList;
}

-(void)containsPoint:(CGPoint)point complete:(ConatinsPointBlock)hasConatainsBlock{
    for (NSDictionary *dic in self.pathInfoMArr) {
        UIBezierPath *path = dic[@"path"];
        if ([path containsPoint:point]) {
            hasConatainsBlock(dic[@"roomId"]);
        }
    }
}

-(NSMutableArray *)pathInfoMArr{
    if (!_pathInfoMArr) {
        _pathInfoMArr = [NSMutableArray new];
    }
    return _pathInfoMArr;
}

@end
